<?php

namespace App\Http\Controllers\Admin\Setting;

use App\Http\Controllers\Admin\Controller;
use App\Models\WebSiteConfig;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;

class WebSiteController extends Controller
{
    public function index(): Response
    {
        $data = WebSiteConfig::query()->pluck('value', 'key');

        return $this->inertia('Admin/Setting/WebSite/Index', [
            'data' => $data,
        ]);
    }

    public function update(): RedirectResponse
    {
        $params = request()->validate([
            'name' => 'required|string',
            'short_name' => 'required|string',
            'title' => 'required|string',
            'keywords' => 'required|string',
            'description' => 'required|string',
            'logo' => 'required|string',
            'admin_logo' => 'required|string',
            'admin_name' => 'required|string',
            'copyright' => 'required|string',
            'icp' => 'required|string',
            'contact_name' => 'nullable|string',
            'contact_phone' => 'nullable|string',
            'contact_email' => 'nullable|string',
            'contact_address' => 'nullable|string',
            'qq_qrcode' => 'nullable|string',
            'wechat_qrcode' => 'nullable|string',
            'wechat_work_qrcode' => 'nullable|string',
        ]);

        foreach ($params as $key => $value)
        {
            WebSiteConfig::query()->where('key', $key)->update(['value' => $value]);
        }
        WebSiteConfig::clearCache();
        return redirect()->back()->with('message', '保存成功');
    }
}
